package com.atguigu.guli.service.sms.service.impl;

import com.atguigu.guli.service.base.exception.GuliException;
import com.atguigu.guli.service.base.result.ResultCodeEnum;
import com.atguigu.guli.service.sms.service.SmsService;
import com.atguigu.guli.service.sms.util.SmsProperties;
import com.atguigu.guli.service.sms.utils.HttpUtils;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author fenge
 */

@Slf4j
@Service
public class SmsServiceImpl implements SmsService {
    @Autowired
    private SmsProperties smsProperties;

    @Override
    public void send(String mobile, String checkCode) throws Exception {
        String host = smsProperties.getHost();
        String path = smsProperties.getPath();
        String method = smsProperties.getMethod();
        String appcode = smsProperties.getAppCode();
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("mobile", mobile);
        querys.put("param", "**code**:"+checkCode+",**minute**:5");
        querys.put("smsSignId", smsProperties.getSmsSignId());
        querys.put("templateId", smsProperties.getTemplateId());
        Map<String, String> bodys = new HashMap<String, String>();
        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            System.out.println(response.toString());
            //获取response的body
            Map resultMap = new Gson().fromJson(EntityUtils.toString(response.getEntity(), "UTF-8"), Map.class);
            System.out.println(resultMap.get("code").getClass().getName());
            System.out.println(resultMap);
            String code = (String)(resultMap.get("code"));
            if ("1204".equals(code)) {
                log.error("签名未报备");
                throw new GuliException(ResultCodeEnum.SMS_SEND_ERROR_INVALID_PHONENUM);
            }
            if ("1205".equals(code)) {
                log.error("签名不可用");
                throw new GuliException(ResultCodeEnum.SMS_SEND_ERROR_INVALID_PHONENUM);
            }
            if ("1302".equals(code)) {
                log.error("短信内容包含敏感词");
                throw new GuliException(ResultCodeEnum.SMS_SEND_ERROR_INVALID_PHONENUM);
            }
            if ("1304".equals(code)) {
                log.error("短信内容过长");
                throw new GuliException(ResultCodeEnum.SMS_SEND_ERROR_INVALID_PHONENUM);
            }
            if ("1320".equals(code)) {
                log.error("模板ID不存在");
                throw new GuliException(ResultCodeEnum.SMS_SEND_ERROR_INVALID_PHONENUM);
            }
            if ("1403".equals(code)) {
                log.error("手机号码不正确");
                throw new GuliException(ResultCodeEnum.SMS_SEND_ERROR_INVALID_PHONENUM);
            }
            if ("1905".equals(code)) {
                log.error("验证未通过");
                throw new GuliException(ResultCodeEnum.SMS_SEND_ERROR_INVALID_PHONENUM);
            }
            if (!"0".equals(code)) {
                log.error("短信发送失败 " + " - code: " + code);
                throw new GuliException(ResultCodeEnum.SMS_SEND_ERROR);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
